<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\tool\Result;
use think\Response;
use think\response\Json;

class Spec extends Base
{
    protected $pk = 'id';

    protected $model = '\app\common\model\Spec';

    protected $append = ['specText'];

    public function index()
    {
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'barDemo,140')
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addExtraHtml(['spec'])
            ->addSearch([
                ['title', '规格模板名称', 'input,Like'],
            ])
            ->fetch();
    }

    public function add($tpl = '')
    {
        return parent::add('spec/edit'); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        return parent::edit($id, 'spec/edit', $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public function getSpec($id = '', $type = 1): Response
    {
        if ($type == 1) {
            $list = $this->model::where('id', $id)->append(['specText'])->find();
        } else {
            $list = $this->model::field('id,title')->order('create_time', 'desc')->select();
        }
        return Result::success('success', $list);
    }
}
